var xmlhttp;
var savedRecommandId;
var savedTitle;
var savedStatus;


function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function addMachine(examType)
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var update = document.getElementById("update").value;
	var machineType = document.getElementById("machineType").value;
	var content = document.getElementById("content").value;
	
	var url = "../controllers/addMachine.php";
	var params = "title=" + title + "&update=" + update + "&machineType=" + machineType + "&content=" + encodeURIComponent(content) + "&examType=" + examType; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = addMachineSuccess;
	xmlhttp.send(params);
}

function addMachineSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("添加机经成功");
			window.location.href=window.location.href;
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function updateListTitle(mlId)
{
	getXmlHttpObject();
	var title = document.getElementById("listTitle").value;
	
	var url = "../controllers/updateMachineList.php";
	var params = "title=" + title + "&mlId=" + mlId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = updateListTitleSuccess;
	xmlhttp.send(params);
}

function updateListTitleSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("更新机经标题成功");
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteMachine(machineId)
{
	getXmlHttpObject();
	var title = document.getElementById("listTitle").value;
	
	var url = "../controllers/deleteMachine.php";
	var params = "machineId=" + machineId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteMachineSuccess;
	xmlhttp.send(params);
}

function deleteMachineSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			location.reload();
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function gotoUpdateMachine(machineId)
{
	var requestUrl = "../views/editMachine.php?machineId=" + machineId;
	window.location.href=requestUrl;
}

function updateMachine(machineId)
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var update = document.getElementById("update").value;
	var machineType = document.getElementById("machineType").value;
	var content = document.getElementById("content").value;
	
	var url = "../controllers/updateMachine.php";
	var params = "title=" + title + "&update=" + update + "&content=" + encodeURIComponent(content) + "&machineType=" + machineType + "&machineId=" + machineId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = updateMachineSuccess;
	xmlhttp.send(params);
}

function updateMachineSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("更新机经成功");
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}